#include "Sock.hpp"
#include <cstdio>
#include <cstdlib>

void TcpConnect(int listen_sockfd, uint16_t port)
{
    Sock::Bind(listen_sockfd, port);
    Sock::Listen(listen_sockfd);
    int sockfd = Sock::Accept(listen_sockfd);

    sleep(100);
    close(sockfd);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s port\n", argv[0]);
        exit(1);
    }

    int fd[5];
    int listen_sockfd = Sock::Socket();
    for (int i = 0; i < 5; i++)
    {
        fd[i] = fork();
        if (fd[i] == 0)
        {
            //建立连接
            TcpConnect(listen_sockfd, atoi(argv[1]));
            return 0;
        }
        if (fd[i] < 0)
        {
            fprintf(stderr, "fork: %s", strerror(errno));
            exit(6);
        }
    }

    //等待进程
    for (int i = 0; i < 5; i++)
    {
        wait(nullptr);
    }

    close(listen_sockfd);
    return 0;
}